﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

public class PageDataList
{
    /// <summary>
    /// 对DataTable进行分页,起始页为1
    /// </summary>
    /// <param name="dt">原始数据源</param>
    /// <param name="PageIndex">当前页序号</param>
    /// <param name="PageSize">页大小</param>
    /// <param name="PageCount">返回页数</param>
    /// <returns></returns>
    public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize, out int PageCount)
    {
        if (dt != null && dt.Rows.Count > PageSize)
        {
            PageCount = getPageCount(dt.Rows.Count, PageSize);
            if (PageIndex == 0)
                return dt;
            DataTable newdt = dt.Clone();

            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
                return newdt;

            if (rowend > dt.Rows.Count)
                rowend = dt.Rows.Count;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }
            return newdt;
        }
        else
        {
            if (dt == null)
                PageCount = 0;
            else
                PageCount = dt.Rows.Count;
            return dt;
        }
    }

    /// <summary>
    /// 对DataTable进行分页,起始页为1(重载 )
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="PageIndex"></param>
    /// <param name="PageSize"></param>
    /// <returns></returns>
    public static DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)
    {
        if (dt != null && dt.Rows.Count > PageSize)
        {
            if (PageIndex == 0)
                return dt;
            DataTable newdt = dt.Clone();

            int rowbegin = (PageIndex - 1) * PageSize;
            int rowend = PageIndex * PageSize;

            if (rowbegin >= dt.Rows.Count)
                return newdt;

            if (rowend > dt.Rows.Count)
                rowend = dt.Rows.Count;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }
            return newdt;
        }
        else
        {
            return dt;
        }
    }

    /// <summary>
    /// 获取总页数
    /// </summary>
    /// <param name="sumCount">结果集数量</param>
    /// <param name="pageSize">页面数量</param>
    /// <returns></returns>
    private static int getPageCount(int sumCount, int pageSize)
    {
        int page = sumCount / pageSize;
        if (sumCount % pageSize > 0)
        {
            page = page + 1;
        }
        return page;
    }


}
